<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar title="搜索结果" left-arrow @click-left="$router.go(-1)" fixed />
    </div>
    <!-- 文章列表 -->
    <div>
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad" :immediate-check="false">
        <ArticleItem v-for="(item,index) in articleList" :key='index' :art='item' @click.native="itemClickFn(item.art_id)"/>
      </van-list>
    </div>
  </div>
</template>

<script>
import {
  searchResultAPI
} from '@/api'
import ArticleItem from '@/views/Home/components/ArticleItem.vue'
export default {
  name: 'SearchResult',
  data () {
    return {
      loading: false, // 加载状态
      finished: false, // 是否全部加载完成
      articleList: [] // 文章数据信息
    }
  },
  methods: {
    // 触底加载更多
    onLoad () {
      if (this.articleList.length > 0) {
        this.getArticleList()
        this.loading = false
      }
    },
    // 获取文章数据信息
    async getArticleList () {
      const result = await searchResultAPI(this.$route.params.keywords)
      // 没有数据了
      if (result.data.data.results.length === 0) {
        this.finished = true
        return
      }
      this.articleList = [...this.articleList, ...result.data.data.results]
    },
    // 单元格点击事件
    itemClickFn (id) {
      this.$router.push({
        path: `/article_detail?art_id=${id}`
      })
    }
  },
  components: {
    ArticleItem
  },
  created () {
    this.getArticleList()
  }
}

</script>

<style lang="less" scoped>
  .search-result-container {
    padding-top: 46px;
  }

</style>
